package zheezes.protocol.impl.gbw.afn8d;

import java.util.ArrayList;
import java.util.List;

import zheezes.protocol.impl.gbw.DataUnit;
import zheezes.protocol.impl.gbw.bean.VipFrozenData;
import zheezes.util.ByteUtil;


public class Afn8DUpF2DataUnit extends DataUnit {
	private short amount;
	private List<VipFrozenData> list = new ArrayList<VipFrozenData>();

	public short getAmount() {
		return amount;
	}

	public void setAmount(short amount) {
		this.amount = amount;
	}

	public VipFrozenData[] getDatas() {
		return list.toArray(new VipFrozenData[0]);
	}

	public void addData(VipFrozenData data) {
		list.add(data);
	}

	@Override
	public int sizeOfData() {
		int size = 2;
		for (VipFrozenData vfd : list) {
			size += vfd.sizeof();
		}
		return size;
	}

	@Override
	public int getData(byte[] buf, int offset) {
		int idx = offset;
		ByteUtil.stoc(buf, idx, amount);
		idx += 2;
		for (VipFrozenData vfd : list) {
			idx += vfd.getBytes(buf, idx);
		}

		return idx - offset;
	}

	@Override
	public int setData(byte[] buf, int offset, int max) {
		int idx = offset;
		amount = ByteUtil.ctos(buf, idx);
		idx += 2;
		for (int i = 0; i < amount && idx < max; i++) {
			VipFrozenData vfd = new VipFrozenData();
			idx += vfd.setBytes(buf, idx);
			list.add(vfd);
		}

		return idx - offset;
	}
}
